package com.bootcamp.servlet.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebFilter(value = "/loginInterface.html")
public class LoginInterfaceFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req =  (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        if (session.getAttribute("userId")!=null){
            PrintWriter writer = response.getWriter();
            String storepage = req.getContextPath() + "/ShopStoreServlet";
            writer.println("您已经登录！正在跳转购物页面ing");
            resp.setHeader("refresh", "1;url=" + storepage);
            return;
        }
        chain.doFilter(request, response);
    }
}
